package com.dajie.pt.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dajie.common.dictionary.DictEnum;
import com.dajie.common.dictionary.model.DictItem;
import com.dajie.common.framework.spring.controller.BaseController;
import com.dajie.common.util.StringUtil;
import com.dajie.core.profile.service.ProfileService;
import com.dajie.corp.api.service.CorpService;
import com.dajie.infra.user.model.UserBase;
import com.dajie.pt.model.InvitationApply;
import com.dajie.word.filter.model.FilterWord;
import com.dajie.word.filter.service.KeywordFilterService;

/**
 * 
 * @author panbing.wu
 * 
 */
public class InvitationBaseController extends BaseController
{

	protected static final Logger LOGGER = LoggerFactory.getLogger(InvitationBaseController.class);

	protected static final int NOT_DICT_ITEM_ID = 0;

	protected static final int MAX_NAME_LENGTH = 30;
	protected static final int MIN_NAME_LENGTH = 4;
	protected static final int MAX_DESC_LENGTH = 400;
	protected static final int MIN_DESC_LENGTH = 10;
	protected static final int MAX_TAGS_LENGTH = 40;
	protected static final int MIN_CORP_NAME_LENGTH = 2;
	protected static final int MAX_CORP_NAME_LENGTH = 40;
	protected static final int MIN_SCHOOL_NAME_LENGTH = 2;
	protected static final int MAX_SCHOOL_NAME_LENGTH = 40;
	protected static final int MAX_CREATE_GROUP_COUNT = 5;
	protected static final int MAX_JOIN_GROUP_COUNT = 50;
	protected static final int MAX_GROUP_COUNT = MAX_CREATE_GROUP_COUNT + MAX_JOIN_GROUP_COUNT;
	protected static final int MAX_SPREAD_COUNT = 50;

	@Autowired
	private KeywordFilterService keywordFilterService;

	@Autowired
	protected ProfileService profileService;

	@Autowired
	protected CorpService corpService;

	protected boolean checkWord(String word)
	{
		return StringUtil.isEmpty(word) || keywordFilterService.checkByLevel(word, FilterWord.FilterLevel.FIRST);
	}

	protected String getCityIdsAndNames(String cityIds)
	{
		if (StringUtil.isEmpty(cityIds))
		{
			return "";
		}
		Map<Integer, DictItem> cityMap = getDictMap().get(DictEnum.DICT_CITY.name()).getMap();
		StringBuilder stringBuilder = new StringBuilder();
		List<String> cityIdList = StringUtil.splitStringByDelimiter(cityIds, ",", "[0-9]+");
		for (String cityId : cityIdList)
		{
			if ((StringUtil.isInteger(cityId)) && (cityMap.containsKey(Integer.valueOf(cityId))))
			{
				String name = cityMap.get(Integer.valueOf(cityId)).getName();
				stringBuilder.append(cityId).append(",").append(name).append(",");
			}
		}
		if (stringBuilder.length() > 0)
		{
			stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		}
		return stringBuilder.toString();
	}
	
	protected InvitationApply getInvitationApplyByUid(HttpServletRequest request,Integer uid)
	{
		Object uid_invitation = request.getSession().getAttribute(uid.toString());
		InvitationApply invitationApply =null;
		if(uid_invitation == null)
		{
			invitationApply = new InvitationApply();
			invitationApply.setUid(uid);
			request.getSession().setAttribute(uid.toString(), invitationApply);
		}else
		{
			invitationApply = (InvitationApply) uid_invitation;
		}
		return invitationApply;
	}
	
	protected InvitationApply getInvitationApplyByMobile(HttpServletRequest request,String mobile)
	{
		Object mobile_invitation = request.getSession().getAttribute(mobile);
		InvitationApply invitationApply =null;
		if(mobile_invitation == null)
		{
			invitationApply = new InvitationApply();
			invitationApply.setMobile(mobile);
			request.getSession().setAttribute(mobile, invitationApply);
		}else
		{
			invitationApply = (InvitationApply) mobile_invitation;
		}
		return invitationApply;
	}


	protected UserBase getUserBase()
	{
		return getLoginUser();
	}
}
